package org.example.k8s_cloudplatform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.kubernetes.client.openapi.ApiException;
import jakarta.annotation.Resource;
import org.example.k8s_cloudplatform.K8S_mapper.K8SPodMapper;
import org.example.k8s_cloudplatform.K8S_mapper.ServiceMapper;
import org.example.k8s_cloudplatform.entity.Pod;
import org.example.k8s_cloudplatform.entity.PodAccess;
import org.example.k8s_cloudplatform.entity.User;
import org.example.k8s_cloudplatform.entity.res.Result;
import org.example.k8s_cloudplatform.mapper.PodAccessMapper;
import org.example.k8s_cloudplatform.mapper.PodMapper;
import org.example.k8s_cloudplatform.mapper.UserMapper;
import org.example.k8s_cloudplatform.service.Impl.PodServiceImpl;
import org.example.k8s_cloudplatform.utils.CheckUtil;
import org.example.k8s_cloudplatform.utils.EmailVerifyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class PodService extends ServiceImpl<PodMapper, Pod> implements PodServiceImpl {
    @Resource
    private PodMapper podMapper;
    @Resource
    private PodAccessMapper podAccessMapper;
    @Resource
    private UserMapper userMapper;

    @Resource
    private K8SPodMapper k8sPodMapper;
    @Resource
    private ServiceMapper serviceMapper;
    @Transactional
    public Result delete(Integer podID,Integer status) {//0:主动注销,1:到期回收
        Pod pod = podMapper.selectById(podID);
        //K8S操作
        try {
            serviceMapper.delete(pod);
        } catch (ApiException e) {
            e.printStackTrace();
            System.out.println(e.getResponseBody());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.exception();
        }
        try {
            k8sPodMapper.delete(pod);
        } catch (ApiException e) {
            e.printStackTrace();
            System.out.println(e.getResponseBody());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.exception();
        }
        //数据库操作并发送邮件
        pod.setStatus(1);
        podMapper.updateById(pod);
        //拥有者
        User owner = userMapper.selectById(pod.getUserID());
        if(owner.getEmail()!=null&& CheckUtil.isEmail(owner.getEmail())&&status==1){
            //发送邮件
            EmailVerifyUtil.sendMailImmediately(owner.getEmail(),"山东大学Docker云平台通知",
                    "您的Docker(ID:"+podID+")已经过期，所有资源已被释放。<br>" +
                            "如有疑问请联系平台管理员");
        }
        //访问者
        LambdaQueryWrapper<PodAccess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PodAccess::getDockerID, podID);
        List<PodAccess> podAccessList = podAccessMapper.selectList(queryWrapper);
        for (PodAccess podAccess : podAccessList) {
            String userMail =userMapper.selectById(podAccess.getUserID()).getEmail();
            EmailVerifyUtil.sendMailImmediately(userMail,"山东大学Docker云平台通知",
                    "您对Docker(ID:" +podID+")的访问权限因原Docker注销而被回收。<br>" +
                            "如有疑问请联系平台管理员。");
            podAccessMapper.deleteById(podAccess);
        }
        return Result.success();
    }
}
